<?php

namespace app\models;

use yii;
use yii\db\Query;

class Staff extends BaseModel
{
    use \app\lib\Redis;

    private $redis;

    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix . 'staff';
        $this->redis = $this->getInstance();
    }

    public function all($where = [], $offset = false, $limit = false, $order = false)
    {
        $db = new Query();
        $db->select('a.*,b.group_name')->from($this->table . ' as a')
            ->join('LEFT JOIN', yii::$app->db->tablePrefix . "department as b", 'a.group_id=b.id');

        if ((!empty($where['status'])) || (isset($where['status']) && $where['status'] == 0)) {
            $db->andWhere(['a.status' => $where['status']]);
        }
        if ($order) $db->orderBy($order);
        return $db->all();
    }

    public function one($where = [])
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['job_number'])) {
            $db->andWhere(['job_number' => $where['job_number']]);
        }
        return $db->limit(1)->one();
    }

    public function get_staff_list($where = [])
    {
        $db = new Query();
        $db->select('a.*,b.group_name')
            ->from(yii::$app->db->tablePrefix . "staff as a")
            ->join('LEFT JOIN', yii::$app->db->tablePrefix . "department as b", 'a.group_id=b.id');
        if (!empty($where['status']) || (isset($where['status']) && $where['status'] == 0)) {
            $db->andWhere(['status' => $where['status']]);
        };

        $list = $db->orderBy(['a.date' => SORT_DESC])
            ->all();
        return $list;
    }


    public function get_max_job_number()
    {
        return (new yii\db\Query)->select('MAX(job_number) as num')
            ->from($this->table)
            ->limit(1)
            ->one();
    }

    /**获取所有业务员
     * @return array|bool|mixed|string
     */
    public function get_all_saleman()
    {
        // $redis_key = 'staff:all';
        // if ($list = $this->redis->get($redis_key)) {
        //     return json_decode($list, true);
        // } else {
            $list = (new yii\db\Query)->select('job_number,name,en_name,email,date as in_date')
                ->from($this->table)
                ->orderBy(['group_id' => SORT_ASC, 'id' => SORT_ASC])
                ->all();
            // $this->redis->setex($redis_key, 3600 * 24, json_encode($list));
            return $list;
        // }

    }

    public function get_purchases_saleman()
    {
        //1 业务 2 采购
        $db = new Query();
        $list = $db->select('*')
            ->from($this->table)
            ->where(['group_id' => 2, 'status' => 0])
            ->orWhere(['group_id' => 1, 'status' => 0])
            ->all();
        $data = ['purchase' => [], 'saleman' => []];
        $db = new Query();
        $relation_list = $db->select('*')
            ->from(yii::$app->db->tablePrefix . 'purchase_saleman')
            ->all();
        foreach ($list as $k => $v) {
            if ($v['group_id'] == 2) {
                $data['purchase'][$k] = $v;
                $data['purchase'][$k]['saleman'] = [];
                foreach ($relation_list as $v2) {
                    if ($v['job_number'] == $v2['purchase']) {
                        $data['purchase'][$k]['saleman'] = $v2['saleman_list'];
                    }
                }
            } else {
                $data['saleman'][] = $v;
            }
        }
        return $data;
    }

    public function update_purchase_saleman($data, $purchase)
    {
        $table = yii::$app->db->tablePrefix . 'purchase_saleman';
        //查询是否存在
        $db = new Query();
        $re = $db->select('id')->from($table)->where(['purchase' => $purchase])->one();
        if ($re) {
            //存在直接更新
            $info = yii::$app->db->createCommand()->update($table, ['saleman_list' => $data], ['purchase' => $purchase])->execute();
        } else {
            //不存在insert
            $info = yii::$app->db->createCommand()->insert($table, ['purchase' => $purchase, 'saleman_list' => $data])->execute();
        }
        if ($info) {
            return 1;
        }

        return 0;
    }


    /**
     * 获取工号
     * @param int $id
     * @return array
     */
    public function getJobNumber($staff_id)
    {
        $key = 'staff';
        if (!$data = $this->redis->hget($key, $staff_id)) {
            $db = new Query();
            $info = $db->select('*')->from($this->table)->where(['id' => $staff_id])->limit(1)->one();
            if ($info) {
                $this->redis->hset($key, $info['id'], json_encode($info));
            }
            return $info;
        }
        return json_decode($data, true);
    }

    /**
     * 员工列表
     * @return array
     */
    public function staff_list()
    {
        $db = new Query();
        $list = $db->select('*')->from($this->table)->all();
        return $list;
    }

    /**通过组获取员工编号
     * @return array
     */
    public function getStaffByGroup($id)
    {
        $db = new Query();
        $list = $db->select('*')->from($this->table)->where(['group_id' => $id])->all();
        $arr = [];
        foreach ($list as $v) {
            $arr[] = $v['job_number'];
        }
        return $arr;
    }

}
